//import { getGoodsData, getCategories } from '../../api/api.js';
import { getServiceHotline } from '../../request/home';
const app = getApp()
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    categoryData:[
      {
        id:0,
        imgUrl:'/assets/image/xyy_dianzi.png',
        title:'电子钱包'
      },
      {
        id:1,
        imgUrl:'/assets/image/xyy_baoyue.png',
        title:'包月卡'
      },
      {
        id:2,
        imgUrl:'/assets/image/xyy_yun.png',
        title:'云卡'
      },
    ]
    
  },


  /** 扫码事件**/
  scanCodeEvent: function(){
    var that = this;
    wx.scanCode({
      onlyFromCamera: true,// 只允许从相机扫码
      success(res){
        console.log("扫码成功："+JSON.stringify(res))
        // 扫码成功后  在此处理接下来的逻辑
        let index = res.result .lastIndexOf("=")
        res.result =res.result .substring(index+1,res.result.length);
        that.setData({
          scanCode: res.result//扫描得到的结果
        })
        console.log(that.data.scanCode)
        //跳转商品列表页面
        wx.navigateTo({
          url: '../index/index?scanCode=' + that.data.scanCode
        })
      },
      fail (err) {
        console.log(err)
        wx.showToast({
          title:'扫描失败',
          icon: 'none',
          duration: 1000
        })
      }
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.login()
  },

  login() {
    wx.login({
      success: (res) => {
        if (res.code) {
          console.log('Login code:', res.code);
          this.requestHttp('/app/auth/wechat/login', { code: res.code }, 'get').then(({ data }) => {
              const app = getApp();
              app.globalData.token = 'Bearer ' + data.token;
              app.globalData.type = data.type;
              console.log(app.globalData.token)
          });
        }
      }
    });
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
  },
 
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
 
  },

  requestHttp(url,data,method){
    return new Promise(function (reslove, reject) {
      console.log(app.globalData.token)
      let headers = {
        'Authorization': app.globalData.token || ''
      };
      wx.request({
        url: app.globalData.host + url,
        method: method || 'get',
        data: data,
        header: headers,
        success: function success(res) {
          const data = res.data;
          if (data.code * 1 === 0) {
            reslove(data)
          } else {
            wx.showToast({
              title: data.msg || '网络异常',
              icon: 'error',
            })
            setTimeout(() => {
              reject(data);
            }, 10)
          }
          // reslove(res)
        },
        fail: function fail(res) {
          wx.showToast({
            title: res,
            icon: 'error',
          })
          reject(res);
        }
      })
    })
  }
})